package com.wf.smartcomm.server.admin.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.wf.smartcomm.server.apns.IApnsService;
import com.wf.smartcomm.server.apns.model.Payload;
import com.wf.smartcomm.server.bean.Message;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.model.PushMessage;
import com.wf.smartcomm.server.push.Pusher;
import com.wf.smartcomm.server.service.HouseService;
import com.wf.smartcomm.server.service.MessageService;
import com.wf.smartcomm.server.service.UserService;
import com.wf.smartcomm.server.util.ConstantUtils;
import com.wf.smartcomm.server.util.PushUtils;

@Controller
@Scope("prototype")
public class PushAction extends BaseAction {
	
	@Resource
	private MessageService msgServiceImpl;
	
	@Resource
	private HouseService houseServiceImpl;
	
	@Resource
	private UserService userServiceImpl;

	public void toUser() throws Exception{
		User user = (User) session.getAttribute("loginUser");
		String uid = request.getParameter("uid");
		String message = request.getParameter("message");
		Message msg = new Message();
		msg.setUser(user);
		msg.setVillage(user.getLoginVillage());
		msg.setContent(message);
		msg.setToId(uid);
		msg.setStatus(0);
		msgServiceImpl.save(msg);
		printJsonData(200);
		User toUser = userServiceImpl.getUserById(uid);
		if (toUser.getDeviceType().equals(ConstantUtils.DEVICE_TYPE_ANDROID)) {
			Pusher pusher = PushUtils.getClientPusher();
			PushMessage pmsg = new PushMessage();
			pmsg.setPushType(ConstantUtils.PUSH_TYPE_PUSH_MSG);
			pmsg.setData(msg.getObjectId());
			pusher.push0x20Message(toUser.getPushId().getBytes(), outGson.toJson(pmsg).getBytes());
			pusher.close();
		} else if(toUser.getDeviceType().equals(ConstantUtils.DEVICE_TYPE_APPLE)){
			IApnsService service = PushUtils.getApnsService();
			Payload payload = new Payload();
			payload.setAlert(msg.getContent());
			payload.setBadge(0);
			payload.setSound("default");
			payload.addParam("objectId", msg.getObjectId());
			payload.addParam("type", ConstantUtils.PUSH_TYPE_PUSH_MSG);
			service.sendNotification(toUser.getAppleToken(), payload);
		}
	}
	
	public void toHouse() throws Exception{
		User user = (User) session.getAttribute("loginUser");
		String houseId = request.getParameter("houseId");
		String message = request.getParameter("message");
		userServiceImpl.listByHouse(houseId,page);
		List<User> users = (List<User>)page.getDataList();
		Message msg = new Message();
		msg.setUser(user);
		msg.setVillage(user.getLoginVillage());
		msg.setContent(message);
		Pusher pusher = PushUtils.getClientPusher();
		IApnsService service = PushUtils.getApnsService();
		PushMessage pmsg = new PushMessage();
		pmsg.setPushType(ConstantUtils.PUSH_TYPE_PUSH_MSG);
		for (User user2 : users) {
			msg.setToId(user2.getObjectId());
			msgServiceImpl.save(msg);
			if (user2.getDeviceType().equals(ConstantUtils.DEVICE_TYPE_ANDROID)) {
				pmsg.setData(msg.getObjectId());
				pusher.push0x20Message(user2.getPushId().getBytes(), outGson.toJson(pmsg).getBytes());
			} else if(user2.getDeviceType().equals(ConstantUtils.DEVICE_TYPE_APPLE)){
				Payload payload = new Payload();
				payload.setAlert(msg.getContent());
				payload.setBadge(0);
				payload.setSound("default");
				payload.addParam("objectId", msg.getObjectId());
				payload.addParam("type", ConstantUtils.PUSH_TYPE_AD_MSG);
				service.sendNotification(user2.getAppleToken(), payload);
			}
			
		}
		pusher.close();
		printJsonData(200);
	}
}
